<?php

namespace Ruixs\AdminApi;

use Illuminate\Support\Facades\Route;
use Ruixs\AdminApi\Models\Admin;

class AdminApiServiceProvider extends \Illuminate\Support\ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'ruixs/adminapi');
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
        $this->buildRoutingCallback();

        if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__.'/../config/adminapi.php' => config_path('adminapi.php'),
            ], 'config');

            $this->publishes([
                __DIR__.'/../resources/lang' => resource_path('lang/vendor/malong'),
            ], 'lang');

            $this->publishesMigrations([
                __DIR__.'/../database/migrations/' => database_path('migrations/ruixs/adminapi'),
            ], 'migrations');
        }
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        config([
            'auth.providers.adminapi' => array_merge([
                'driver' => 'eloquent',
                'model' => Admin::class,
            ], config('auth.providers.admin', [])),
        ]);
        config([
            'auth.guards.adminapi' => array_merge([
                'driver' => 'session',
                'provider' => 'adminapi',
            ], config('auth.guards.admin', [])),
        ]);
        if (! app()->configurationIsCached()) {
            $this->mergeConfigFrom(__DIR__.'/../config/adminapi.php', 'adminapi');
        }
    }

    protected function buildRoutingCallback()
    {
        if (! app()->routesAreCached()) {
            Route::prefix(config('adminapi.route.prefix'))->group(function () {
                $this->loadRoutesFrom(__DIR__.'/../routes/adminapi.php');
            });
        }
    }
}